package com.dashan.backend.datastore;

import java.util.List;

import javax.jdo.Query;

import com.dashan.backend.datastore.DsSession.Status;


public class SessionDao extends DaoBase {

    @SuppressWarnings("unchecked")
    public DsSession getLastSession(String userName, Status status) {
        Query query = pm.newQuery(DsSession.class);
        query.setFilter("userName == userParam && statusName == statusParam");
        query.setOrdering("timestamp desc");
        query.declareParameters("String userParam, String statusParam");
        query.setRange(0, 1);
        
        List<DsSession> result = (List<DsSession>) query.execute(userName, status.getName());
        if (result.size() == 0) {
            return null;
        } else {
            return result.get(0);
        }
    }
    
    /**
     * Create a session in datastore.
     * @param dsSession
     * @return the detached copy of the dsSession object
     */
    public void createSession(DsSession dsSession) {
        pm.makePersistent(dsSession);
    }
    
    public void updateSession(DsSession dsSession) {
    }
}
